package se.zens.common;

import java.io.File;
import java.io.IOException;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import se.zens.common.exception.ConfigurationException;

public class ZensEnvironment
{
	private static String systemBaseDir = null;
	
	private static String baseLogDir = null;
	
	private static final String ZENS_CONF_DIR = "ZENS_CONF_DIR";
	private static final String ZENS_LOG_DIR = "ZENS_LOG_DIR";
	private static final String CONF_DIR = "conf";
	
	
	private static Logger logger = LogManager.getLogger(ZensEnvironment.class);
	/**
	 * Loads the config properties for a module, throwing an exception if it cannot be found.<br>
	 */
	public static
	PrefixProperties getConfigProperties(String moduleName)
		throws ConfigurationException
	{
		
		File configFile = new File(getConfigDir(moduleName), getConfigFileName(moduleName));
		
		PrefixProperties properties = new PrefixProperties();
		try
		{
			if (configFile.canRead())
			{
				properties.loadFromFile(configFile);
			}
			else
			{
				logger.error("Could not read config file " + configFile.getAbsolutePath() );
				throw new ConfigurationException("Could not read config file " + configFile.getAbsolutePath());
				
			}
		}
		catch (IOException e)
		{
			throw new ConfigurationException("Failed to load module properties");
		}
		return properties;
	}
	
	
	private static String getConfigFileName(String moduleName)
	{
		return moduleName  + ".properties";
	}


	/**
	 * Returns the system base directory<br>
	 * Typically /opt/zens but can be overridden by setting the environment variable (or system property) ZENS_CONF_DIR<br> 
	 *  <br>
	 * @return The system base directory path
	 */
	public static 
	String getSystemBaseDir()
	{
		if (systemBaseDir != null)
			return systemBaseDir;
		systemBaseDir = System.getenv(ZENS_CONF_DIR);
		if (systemBaseDir == null)
			systemBaseDir = System.getProperty(ZENS_CONF_DIR);
		if (systemBaseDir == null)
			systemBaseDir = "/opt/zens";
		return systemBaseDir;
	}
	/**
	 * Returns the directory for a module for a specific purpose (eg conf)<br>
	 * <br>
	 * @param purpose The purpose of the directory to return, eg "conf", "bin" etc 
	 * @param moduleName The name of the module
	 * @return The path to the specified directory
	 */
	static 
	String getModuleDir(String purpose, String moduleName)
	{
		String baseDir = getSystemBaseDir();
		
		return baseDir + File.separator + purpose + File.separator + moduleName;
	}
	
	/**
	 * Returns the system log base directory, typically /var/zens but can be overridden by setting the environment variable ZENS_LOG_DIR<br>
	 *  <br>
	 * @return The system log base directory path
	 */
	static 
	String getSystemBaseLogDir()
	{
		if (baseLogDir != null)
			return baseLogDir;
		
		baseLogDir = System.getenv(ZENS_LOG_DIR);
		if (baseLogDir == null)
			baseLogDir = "/var/zens";
		
		return baseLogDir;
	}
	
	/**
	 * Gets the config directory for a specific module<br> 
	 * <br>
	 * @param moduleName The name of the module
	 * @return The path to use for configuration files for the module
	 */
	public static 
	String getConfigDir(String moduleName)
	{
		return getModuleDir(CONF_DIR, moduleName); 
	}
	
	
}
